package com.css.fxfzypg.common.utils.excel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

//excel导出工具
public class ExportUtil {
    public static <T> void createExcel(HttpServletResponse response, List<T> list, String excelName, Class<T> tClass) throws IOException, IllegalAccessException {
        //生成excel表
        //创建工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //创建工作表对象
        HSSFSheet sheet = workbook.createSheet(excelName);
        sheet.setDefaultColumnWidth(10);
        //创建行
        HSSFRow row = sheet.createRow(0);
        Field[] fields = tClass.getDeclaredFields();
        fields = checkAndSort(fields);
        for (int i = 0; i < fields.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(fields[i].getAnnotation(ExcelPropertyCheck.class).name());
        }
        for (int i = 0; i < list.size(); i++) {
            HSSFRow row1 = sheet.createRow(i+1);
            T t = list.get(i);
            Field[] fields1 = t.getClass().getDeclaredFields();
            fields1 = checkAndSort(fields1);
            for (int j = 0; j < fields1.length; j++) {
                fields1[j].setAccessible(true);
                if (fields1[j].get(t)!=null){
                    HSSFCell cell = row1.createCell(j);
                    cell.setCellStyle(cellStyle);
                    cell.setCellValue(fields1[j].get(t).toString());
                }
            }
        }

        //设置中文文件名称
        String fileName = URLEncoder.encode(excelName,"UTF-8");
        //浏览器默认服务器传过去的是html，不是excel文件
        //设置响应类型:传输内容是流，并支持中文
        response.setContentType("application/octet-stream;charset=UTF-8");
        //设置响应头信息header，下载时以文件附件下载
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".xls");
        //输出流对象
        OutputStream os = response.getOutputStream();
        workbook.write(os);
        //强制刷新
        os.flush();
        os.close();
        workbook.close();
    }

    private static Field[] checkAndSort(Field[] fields){
        List<Field> fields2 =new ArrayList<>();
        for (Field field : fields) {
            if (field.getAnnotation(ExcelPropertyCheck.class)!=null){
                fields2.add(field);
            }
        }
        fields2.sort((o1, o2)->{
            Integer index1 = o1.getAnnotation(ExcelPropertyCheck.class).index();
            Integer index2 = o2.getAnnotation(ExcelPropertyCheck.class).index();
            return index1.compareTo(index2);
        });
        fields = fields2.toArray(new Field[fields2.size()]);
        return fields;
    }
}
